<template>
  <div>
    <!--  面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  卡片试图区域-->
    <el-card>
      <el-row>
        <!--     添加商品-->
        <el-col>
          <el-button :class="['mb','mt']" @click="showCatedialog" type="primary">添加分类</el-button>
        </el-col>
      </el-row>
      <!--      商品列表-->
      <tree-toble :data="cateList" :columns="columns" :selection-type="false" :expand-type="false" show-index
                  index-text="#" border>
        <!--     是否有效-->
        <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!--     排序-->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" type="primary" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!--     操作-->
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="warning" size="mini" icon="el-icon-delete" @click="deleteCate(scope.row.cat_id)">删除
          </el-button>
        </template>
      </tree-toble>
      <!--      分页区域-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum" :page-sizes="[6, 8, 10, 25,20,30]"
                     :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!--    添加商品-->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClose">
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
        <!--      分类名称-->
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <!--      父级分类-->
        <!--    options 指定数据源-->
        <!--    props  指定配置对象-->
        <el-form-item label="父级分类：">
          <el-cascader expand-trigger="hover" :options="parentList"
                       :props="cascaderProps" v-model="selectedKeys"
                       @change="parentCateChange" clearable change-on-select>

          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addCate">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import params from "../Params/params";

export default {
  data() {
    return {
      cateList: [],   //储存商品
      queryInfo: {   //查询条件
        type: 3,        //第一层数据
        pagenum: 1,     //页数
        pagesize: 5    //条数
      },
      total: null,      //总数
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          //自定义模板
          type: 'template',
          //插槽名
          template: 'isOk'
        },
        {
          label: '排序',
          //自定义模板
          type: 'template',
          //插槽名
          template: 'order'
        },
        {
          label: '操作',
          //自定义模板
          type: 'template',
          //插槽名
          template: 'opt'
        }
      ],  //tree 自定义
      addCateDialogVisible: false,  //添加按钮显示隐藏
      addCateForm: {  //将要添加的对象名称
        cat_name: '',        //分类名称
        cat_pid: 0,             //父级分类id
        cat_level: 0              // 分类层级
      },
      addCateFormRules: {
        cat_name: [
          {required: true, message: '请输入分类名称', trigger: 'blur'}
        ]
      },
      parentList: [],        //父级分类商品
      cascaderProps: {     //指定级联选择器配置对象
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedKeys: []     //选中项
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    //异步获取商品数据列表
    async getCateList() {
      const {data: res} = await this.$http.get('categories', {params: this.queryInfo})
      if (res.meta.status !== 200) {
        this.$message.error('获取商品数据列表失败！')
      }
      this.cateList = res.data.result

      this.total = res.data.total

    },
    // 监听长度
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    // 监听页数
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    //显示隐藏对话框
    showCatedialog() {
      this.getParentCate()
      this.addCateDialogVisible = true
    },
    //获取父级商品
    async getParentCate() {
      const {data: res} = await this.$http.get('categories', {params: {type: 2}})
      if (res.meta.status !== 200) {
        this.$message.error('获取父级商品失败！')
      }
      this.parentList = res.data
    },
    //监听选定项的变化
    parentCateChange() {
      // console.log(this.selectedKeys)
      //父级分类id
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        //父级分类等级赋值
        this.addCateForm.cat_level = this.selectedKeys.length
        return
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    //添加新的分类
    addCate() {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        const {data: res} = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败！')
        }
        this.$message.success('添加分类成功！')
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    //重置对话框
    addCateDialogClose() {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    //删除分类
    async deleteCate(id) {
      const confirmRuselt = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        center: true,
        type: 'warning',
      }).catch(err => err)

      if (confirmRuselt !== 'confirm') {
        return this.$message.info('已取消删除！')
      }
      const {data: res} = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) {
        this.$message.error('删除分类失败！')
      }
      this.$message.success('删除分类成功！')
      this.getCateList()
    }

  }
}
</script>

<style lang="less" scoped>
.mb {
  margin-bottom: 10px;
}

.mt {
  margin-top: -10px;
}

.el-cascader {
  width: 100%;
}
</style>
